package com.yilin.xbr.common.interceptor;

import com.google.gson.Gson;
import com.yilin.xbr.entity.member.Account;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录鉴权拦截器
 */
public class LoginAuthorizeInterceptor implements HandlerInterceptor {
    private Logger log = Logger.getLogger(LoginAuthorizeInterceptor.class);

    private static int loginErrCode = 1001; // 登录错误码
    private static int infoErrCode = 886; // 信息错误码

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
        //AppUtil.clearThreadMap();
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod;
        //配置参数
//        AppUtil.setThreadLocalObject("SESSION", request.getSession());
//        AppUtil.setThreadLocalObject("REQUEST", request);
//        AppUtil.setThreadLocalObject("RESPONSE", response);
        //从头部中获取token对象
        String token = request.getHeader("token");
        String deviceId = request.getHeader("deviceId");
        if (HandlerMethod.class != handler.getClass()) return true;
        handlerMethod = (HandlerMethod) handler;
        //打印地址
        log.info(">>>>>>>>>>>> 小镖人正在请求：" + request.getRequestURI());
        Account account = null;
        if (StringUtils.isNotEmpty(token)) {
//            account = accountService.getAccountByToken(token);
            //保存本次全局账户对象
//            request.setAttribute(ACCOUNT, account);
//            AppUtil.setThreadLocalObject("USER", account);
        }
        //登录信息验证注解
        if (handlerMethod != null) {
            UnInterceptor unInterceptor = handlerMethod.getMethod().getAnnotation(UnInterceptor.class); //不拦截
            if (unInterceptor != null) return  true;
        }
        //验证用户是否登录
        if (StringUtils.isBlank(token)) {
            writeData(response, "您的登录已失效，请重新登录！",loginErrCode);
            return false;
        }
        return true;
    }

    //返回前台数据
    private void writeData(HttpServletResponse response, String data,Integer code) {
        try {
            Map<String, Object> map = new HashMap<>();
            Gson gson = new Gson();
            response.setContentType("text/json");//设置格式为text/json
            response.setCharacterEncoding("UTF-8");//设置字符集为'UTF-8'
            map.put("success", false);
            map.put("code", code);
            map.put("msg",data);
            map.put("data", null);
            PrintWriter write = response.getWriter();
            write.write(gson.toJson(map));
            write.flush();
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

